<template>
  <a-modal
    :title="renderTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :keyboard="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="变量名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <s-input
            v-decorator="[
              'varName',
              {
                rules: [
                  {
                    required: true,
                    message: $t('common.emptyErr', {
                      name: '变量名'
                    }),
                    whitespace: true
                  }
                ]
              }
            ]"
            :limit="128"
          />
        </a-form-item>
        <a-form-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <s-input
            v-decorator="[
              'description',
              {
                rules: [
                  {
                    required: true,
                    message: $t('common.emptyErr', {
                      name: '描述'
                    }),
                    whitespace: true
                  }
                ]
              }
            ]"
            :limit="200"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { SInput } from '@/components'

export default {
  name: 'TypeForm',
  components: { SInput },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      formType: 'ADD',
      id: 0,
      tempId: '',
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  computed: {
    renderTitle() {
      return this.formType === 'ADD' ? '新建内置变量' : '编辑内置变量'
    }
  },
  methods: {
    add(values) {
      this.visible = true
      this.formType = 'ADD'
      this.$nextTick(() => {
        const model = { varName: '', description: '' }
        this.form.setFieldsValue(model)
      })
    },

    edit(values){
      this.visible = true
      this.formType = 'EDIT'
      const {id, tempId, varName, description} = values;
      this.id = id;
      this.tempId = tempId;
      this.$nextTick(() => {
        const model = { varName, description }
        this.form.setFieldsValue(model)
      })
    },

    handleSubmit() {
      const {
        form: { validateFields, getFieldsValue }
      } = this
      validateFields((errors, values) => {
        if (!errors) {
          const { varName, description } = getFieldsValue()
          const model = {
            varName: varName.trim(),
            description: description.trim()
          }
          this.$emit('ok', Object.assign({}, model, { id: this.id, tempId: this.tempId, formType: this.formType }))
          this.handleCancel()
        }
      })
    },

    handleCancel() {
      this.resetForm()
      this.visible = false
    },

    resetForm() {
      this.id = 0
      const originalModel = { varName: '' }
      this.form.setFieldsValue(originalModel)
    }
  }
}
</script>

<style></style>
